package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.*;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LDictType;
import com.learn.core.domain.LExam;
import com.learn.core.domain.LUser;
import com.learn.core.dto.LDictTypeDTO;
import com.learn.core.service.ILDictTypeService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @auther CodeGenerator
 * @create 2021-03-13 19:39:07
 * @describe 字典类型表前端控制器
 */
@Api("字典类型")
@RestController
@RequestMapping("/manager/dtyp")
public class LDictTypeController extends WebController<LDictType> {

    @Autowired
    private ILDictTypeService iLDictTypeService;

    //字典类型树
    @ApiOperation(value = "字典类型树")
    @UserLoginToken
    @PostMapping("/tree")
    public CommonResult<Object> tree(){
        try {
            List<LDictType> lDictTypes = iLDictTypeService.queryLDictTypeAll();
            if(lDictTypes.size()>0)
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS,lDictTypes);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
    }

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LUser> list(@RequestBody LDictTypeDTO lDictTypeDTO){
        try {
            if(lDictTypeDTO.getPage()==null||lDictTypeDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lDictTypeDTO.getPage(), lDictTypeDTO.getRows());
            QueryWrapper<LDictType> queryWrapper = new QueryWrapper<>();
            if(lDictTypeDTO.getDictSys()!=null)
                queryWrapper.eq("dict_sys", lDictTypeDTO.getDictSys());

            if(lDictTypeDTO.getSortType()!=null&&lDictTypeDTO.getSortBy()!=null)
                queryWrapper.orderBy(true,lDictTypeDTO.getSortType(),lDictTypeDTO.getSortBy());

            Page<LDictType> lDictTypes = (Page<LDictType>) iLDictTypeService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lDictTypes)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lDictTypes.getTotal(), lDictTypes, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
        }
    }


    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加字典类型", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LDictType lDictType, HttpServletRequest request){

        try {
            if(lDictType!=null) {
                if (StringUtils.isEmpty(lDictType.getDictTypeName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_DICTTYPE_NAME);
                }

                // token
                String token = request.getHeader("token");
                String userId = JwtUtil.getUserId(token);

                String dictTypeCode = CnToSpell.getPinYinHeadChar(lDictType.getDictTypeName());
                lDictType.setDictTypeCode(dictTypeCode);
                lDictType.setCreateTime(new Date());
                lDictType.setCreateUser(userId);
                if (iLDictTypeService.save(lDictType)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除字典类型", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LDictType lDictType){
        try {
            if(lDictType!=null){
                if(iLDictTypeService.removeById(lDictType.getDictTypeId())){
                    return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e) {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新字典类型", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LDictType lDictType, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lDictType!=null){
            lDictType.setUpdateTime(new Date());
            lDictType.setUpdateUser(userId);
            if(iLDictTypeService.updateById(lDictType)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}

